package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Authority;
import org.millioncall.yueyoga.admin.model.Resource;
import org.millioncall.yueyoga.admin.service.AuthorityService;
import org.millioncall.yueyoga.admin.service.ResourceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;

/**
 * @author lisj 2014年5月24日 下午7:40:09
 */
@Controller
public class AuthorityAction extends BaseAction {

	private AuthorityService authorityService;
	private ResourceService resourceService;

	@RequestMapping(value = "/findAllAuthorities.jhtml")
	public String findAllAuthority(String name, String enabled,
			Integer pageNo, Integer pageSize, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		int start = 0;
		int limit = 10;
		Map<String, String> map = new HashMap<String, String>();
		if (pageSize != null) {
			limit = pageSize;
		}

		if (pageNo != null) {
			start = (pageNo - 1) * limit;
		}

		if (StringUtils.isNotBlank(name)) {
			map.put("name", "%" + name + "%");
			model.put("name", name);
		}

		if (StringUtils.isNotBlank(enabled)) {
			map.put("enabled", enabled);
			model.put("enabled", enabled);
		}
		model.put("authorities",
				authorityService.findAllAuthorities(start, limit, map));
		model.put("resources",resourceService.findResources(true));
		return "authority_search";
	}

	@RequestMapping(value = "/toAddAuthority.jhtml")
	public String toAddAuthority(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		return "authority_add";
	}

	@RequestMapping(value = "/addAuthority.jhtml")
	public void addAuthority(String name, String description,String enabled,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		super.initFrontHeader(request, model);
		String msg = "";
		Authority authority = new Authority();
		if (StringUtils.isNotBlank(name)) {
			authority.setName(name);
		}
		if (StringUtils.isNotBlank(description)) {
			authority.setDescription(description);
		}
		
		if(StringUtils.isNotBlank(enabled)){
			if("1".equals(enabled)){
				authority.setEnabled(true);
			}else{
				authority.setEnabled(false);
			}
		}
		
		if (authorityService.saveAuthority(authority)) {
			msg = "{'success':true, 'msg':'权限创建成功!'}";
		} else {
			msg = "{'success':false, 'msg':'权限创建失败!'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	@RequestMapping(value = "/deleteAuthority.jhtml")
	public void deleteAuthority(String ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		String msg = "";
		if (authorityService.deleteAuthorities(ids)) {
			msg = "{'success':true, 'msg':'权限删除成功!'}";
		} else {
			msg = "{'success':false, 'msg':'权限删除失败!'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	
	@RequestMapping(value = "/findAuthorityResources.jhtml")
	public void findAuthorityResources(Integer auId, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		List<Resource> resources = resourceService.findAuthorityResources(auId);
		String msg = "{'success':true, 'msg':'"+JSONArray.toJSONString(resources)+"'}";
		WebAppUtils.ajaxResponse(response,msg);
	}
	
	@RequestMapping(value = "/grantAuthorityResources.jhtml")
	public void grantAuthorityResources(String rsId,String auId, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		String msg = "";
		if (authorityService.grantAuthorityResources(auId,rsId)) {
			msg = "{'success':true, 'msg':'权限资源指定成功!'}";
		} else {
			msg = "{'success':false, 'msg':'权限资源指定失败!'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	public AuthorityService getAuthorityService() {
		return authorityService;
	}

	public void setAuthorityService(AuthorityService authorityService) {
		this.authorityService = authorityService;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}
	
	/** 检查名字是否存在 */
	@RequestMapping(value="checkAuthorityName.jhtml")
	public void checkName(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String msg = "";
		String name = request.getParameter("name");
		Authority auth = this.authorityService.findAuthorityByName(name);
		if (auth != null) {
			msg = "{'success':false, 'msg':'名称已存在！'}";
		} else {
			msg = "{'success':true, 'msg':'名称可用！'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
}